package com.gewu.pm.ui.adapter.terminal;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gewu.pm.R;
import com.gewu.pm.app.AppAdapter;
import com.gewu.pm.bean.TerminalContactBean;

/**
 * 终端详情 - 联系人列表适配器
 */
public class TerminalDetailContactAdapter extends AppAdapter<TerminalContactBean> {

    private OnContactActionListener mListener;

    public TerminalDetailContactAdapter(Context context) {
        super(context);
    }

    /**
     * 设置操作监听器
     */
    public void setOnContactActionListener(OnContactActionListener listener) {
        mListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ItemViewHolder();
    }

    /**
     * 联系人 Item ViewHolder
     */
    private final class ItemViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView mContactTitleView;
        private final ImageView mChatButton;
        private final ImageView mEmailButton;
        private final ImageView mPhoneButton;
        private final TextView mContactNameView;
        private final TextView mContactPhoneView;
        private final TextView mPrimaryValueView;
        private final TextView mPrimaryTagView;
        private final TextView mSalespersonView;
        private final TextView mEditButton;
        private final TextView mDeleteButton;

        private ItemViewHolder() {
            super(R.layout.terminal_detail_contact_item);
            mContactTitleView = findViewById(R.id.tv_contact_title);
            mChatButton = findViewById(R.id.iv_chat);
            mEmailButton = findViewById(R.id.iv_email);
            mPhoneButton = findViewById(R.id.iv_phone);
            mContactNameView = findViewById(R.id.tv_contact_name);
            mContactPhoneView = findViewById(R.id.tv_contact_phone);
            mPrimaryValueView = findViewById(R.id.tv_primary_value);
            mPrimaryTagView = findViewById(R.id.tv_primary_tag);
            mSalespersonView = findViewById(R.id.tv_salesperson);
            mEditButton = findViewById(R.id.tv_edit);
            mDeleteButton = findViewById(R.id.tv_delete);

            // 右上角三个按钮点击事件
            mChatButton.setOnClickListener(v -> {
                if (mListener != null) {
                    mListener.onChat(getViewHolderPosition());
                }
            });

            mEmailButton.setOnClickListener(v -> {
                if (mListener != null) {
                    mListener.onEmail(getViewHolderPosition());
                }
            });

            mPhoneButton.setOnClickListener(v -> {
                if (mListener != null) {
                    mListener.onPhone(getViewHolderPosition());
                }
            });

            // 编辑按钮
            mEditButton.setOnClickListener(v -> {
                if (mListener != null) {
                    int position = getViewHolderPosition();
                    if (position >= 0 && position < getCount()) {
                        mListener.onEdit(getItem(position), position);
                    }
                }
            });

            // 删除按钮
            mDeleteButton.setOnClickListener(v -> {
                if (mListener != null) {
                    int position = getViewHolderPosition();
                    if (position >= 0 && position < getCount()) {
                        mListener.onDelete(position);
                    }
                }
            });
        }

        @Override
        public void onBindView(int position) {
            TerminalContactBean item = getItem(position);
            
            // 联系人标题
            mContactTitleView.setText("联系人" + getChineseNumber(position + 1));
            
            // 联系人姓名
            mContactNameView.setText(item.getName());
            
            // 联系电话
            mContactPhoneView.setText(item.getPhone());
            
            // 是否主联系人
            if (item.isPrimary()) {
                mPrimaryValueView.setText("是");
                mPrimaryTagView.setVisibility(View.VISIBLE);
            } else {
                mPrimaryValueView.setText("否");
                mPrimaryTagView.setVisibility(View.GONE);
            }
            
            // 业务员（示例数据）
            mSalespersonView.setText("瞿辉");
        }
        
        /**
         * 数字转中文
         */
        private String getChineseNumber(int number) {
            String[] chineseNumbers = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十"};
            if (number > 0 && number <= chineseNumbers.length) {
                return chineseNumbers[number - 1];
            }
            return String.valueOf(number);
        }
    }

    /**
     * 联系人操作监听器
     */
    public interface OnContactActionListener {
        /** 聊天 */
        void onChat(int position);

        /** 邮件 */
        void onEmail(int position);

        /** 电话 */
        void onPhone(int position);

        /** 编辑联系人 */
        void onEdit(TerminalContactBean contact, int position);

        /** 删除联系人 */
        void onDelete(int position);
    }
}

